package com.design.util;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import java.io.*;

/**
 * @author admin
 * @date 2019-01-24 16:21
 */
public class FtpClientUtil {

    private FTPClient ftpClient;
    private String hostname;
    private int port;
    private String username;
    private String password;

    public FtpClientUtil(String hostname, int port, String username, String password) {
        this.hostname = hostname;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    public boolean uploadFile(File file, String remoteDir) {
        connect();
        boolean result;
        try {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            ftpClient.changeWorkingDirectory(remoteDir);

            FileInputStream input = new FileInputStream(file);
            result = ftpClient.storeFile(file.getName(), input);
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return result;
    }

    public boolean uploadFileAutoClassify(File file, String remoteClassifyDir) {
        connect();
        boolean result;
        try {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            String pathname = remoteClassifyDir.concat(file.getName().replaceAll("(.*)(\\.)(.*)", "$3"));

            boolean b = ftpClient.changeWorkingDirectory(pathname);
            if (!b) {
                ftpClient.makeDirectory(pathname);
                ftpClient.changeWorkingDirectory(pathname);
            }

            FileInputStream input = new FileInputStream(file);
            result = ftpClient.storeFile(file.getName(), input);
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return result;
    }

    public boolean downloadFile(String remoteFilePath, File localFile) {
        connect();
        try {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            OutputStream outputStream = new FileOutputStream(localFile);
            boolean b = ftpClient.retrieveFile(remoteFilePath, outputStream);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean connect() {
        ftpClient = new FTPClient();
        if (!ftpClient.isConnected()) {
            try {
                ftpClient.connect(hostname, port);
                if (!(username == null || username.isEmpty() || password == null || password.isEmpty())) {
                    ftpClient.login(username, password);
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    public boolean disconnect() {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }
}
